package com.situ.mall.job;

import com.situ.mall.util.QiniuUtils;
import com.situ.mall.util.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

@Configuration
public class DeleteImageJob {
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 */2 * * * ?")
    public void deleteImage() {
        System.out.println("DeleteImageJob.deleteImage");

        // 根据redis中保存的两个set集合进行差值计算，获得垃圾图片集合
        Set<String> set = redisTemplate.opsForSet().difference(RedisConstant.UPLOAD_IMAGE, RedisConstant.UPLOAD_IMAGE_TO_DB);
        if (set != null) {
            for (String imageName : set) {
                // 删除七牛云服务器上图片
                QiniuUtils.deleteFileFromQiniu(imageName);
                // 从UPLOAD_IMAGE这个集合中删除七牛云上已经删除的图片
                redisTemplate.opsForSet().remove(RedisConstant.UPLOAD_IMAGE, imageName);
                System.out.println("删除图片： " + imageName);
            }
        }
    }
}
